package org.restql.db.operation;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restql.db.Utility;
import org.restql.db.resource.DatabaseResource;
import org.restql.db.resource.ModifiableDatabaseResource;
import org.restql.db.resource.RequestResource;
import org.restql.webapp.ReturnType;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DatabaseInsert extends DatabasePutPost {

	private static Log logger = LogFactory.getLog(DatabaseInsert.class);

	public DatabaseInsert(DatabaseResource databaseResource, RequestResource requestResource) {
		super(databaseResource, requestResource);
	}

	// Node containing children of fields to insert
	protected void doOperation(Map<String, String> fieldValueMap) throws SQLException {
		List<String> fieldList = new ArrayList<String>(fieldValueMap.keySet());
		List<String> valuesList = new ArrayList<String>();
		for (String key : fieldList) {
			valuesList.add(fieldValueMap.get(key));
		}
		
		String constructedSQL =this.databaseResource.getInsertString(this.requestResource, fieldList, valuesList);

		PreparedStatement stmt =
			getConnection().prepareStatement(constructedSQL);

		logger.info("Executing: " + constructedSQL);
		stmt.executeUpdate();
	}

}
